#ifndef __FONT_EDIT_FORM
#define __FONT_EDIT_FORM

#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Buttons.hpp>
#include <Grids.hpp>
#include <ComCtrls.hpp>
#include <Dialogs.hpp>

#include "BkList.h"

//---------------------------------------------------------------------------
typedef void __fastcall (__closure *HFontDecodeEvent)( TObject *Sender, LPBYTE dst, LPCBYTE srs );
typedef void __fastcall (__closure *HFontChangeItemEvent)( TObject *Sender, int idx );

CLASSBASE( HCustomFontBitmap, public TComponent )
  private:
    PHBitmap Bitmap;

  private:
    bool DoLoadCharsFile( CONSTSTR fnm, int count, int charsz, long off, HFontDecodeEvent cb );

  public:
    __fastcall HCustomFontBitmap( TComponent *Owner );
    __fastcall ~HCustomFontBitmap();

    void DoChange( void );
    void DoChangeItem( int idx );

    bool SetCharDump( LPCBYTE dump, int idx );
    bool GetCharDump( LPBYTE dump, int idx );

    void DrawChar( TCanvas *dc, int x, int y, int idx, int zoom = 1 );

    //File
    bool LoadCharsFile( CONSTSTR fnm,
                        int count, int charsz,
                        long off,
                        HFontDecodeEvent cb );
    bool SaveCharsFile( CONSTSTR fnm,
                        int count, int charsz,
                        long off,
                        HFontDecodeEvent cb );
    //Dump
    bool LoadCharsDump( LPBYTE dump,
                        int count, int charsz,
                        HFontDecodeEvent cb );

  //Vars
    MyValArray<TNotifyEvent>         Changes;
    MyValArray<HFontChangeItemEvent> ItemChanges;

  //RO Props
    DEF_PROP_P( int, CharCount,  ; )
    DEF_PROP_P( int, CharWidthB, ; )

  //Props
    DEF_PROP_VV_DEF( int,    CharWidth,  8 )
    DEF_PROP_VV_DEF( int,    CharHeight, 8 )
    DEF_PROP_VV_DEF( TColor, ColorSpace, clWhite )
    DEF_PROP_VV_DEF( TColor, ColorPoint, clBlack )

  //Events
    DEF_PROP_VV( TNotifyEvent,         OnChange )
    DEF_PROP_VV( HFontChangeItemEvent, OnItemChange )
};

CLASSBASE( HFontBitmap, public HCustomFontBitmap )
  public:
    __fastcall HFontBitmap( TComponent *Owner );

  __published:
    __property  CharWidth, CharHeight, ColorSpace, ColorPoint,
                OnChange, OnItemChange;
};

//---------------------------------------------------------------------------
#include "BkList.h"

CLASSBASE( HCustomCharFontList, public HCustomBkList )
  private:
    void __fastcall Change( void );
    void __fastcall idChange( TObject *Sender );
    void __fastcall idItemChange( TObject *Sender, int idx );

  protected:
    virtual void __fastcall Notification( TComponent* p, TOperation op );
    virtual void __fastcall DoDrawItem( TCanvas *dc,int Index, const MyRect& Rect, const TOwnerDrawState& State);
    virtual void __fastcall DoDrawSection( TCanvas *dc,int Index,THeaderSection* p, const MyRect& Rect, const TOwnerDrawState& State );
    virtual void __fastcall DoCorrectOptions( HBkListOptions& NewValue );

  public:
    __fastcall HCustomCharFontList( TComponent *Owner );

  //Props
        DEF_PROP_VP( PHCustomFontBitmap, FontBitmap,  ; )
    DEF_PROP_VP_DEF( bool,               ShowNumbers, ;, true )
    DEF_PROP_VP_DEF( int,                MinHeight,   ;, 19 )
    DEF_PROP_VP_DEF( int,                CharZoom,    ;, 1 )
    DEF_PROP_VP_DEF( int,                CharSection, ;, 0 )
};

CLASSBASE( HCharFontList, public HCustomCharFontList )
  private:

  public:
    __fastcall HCharFontList( TComponent *Owner );

  __published:
  //Base
    DEF_LISTBOX_BASE;
    DEF_BKLIST_PROPS;
    __property OnDrawItem, OnDrawItemBk, OnDrawItemSection,
               OnDrawItemSectionBk, OnSectionMouseDown;
  //Self
    __property FontBitmap, ShowNumbers, MinHeight, CharZoom, CharSection;
};

//---------------------------------------------------------------------------
CLASSBASE( HCustomCharImage, public TGraphicControl )
    HDataPtr CharDump;
    int      ox, oy;

  private:
    void __fastcall idChange( TObject *Sender );
    void __fastcall idItemChange( TObject *Sender, int idx );

  protected:
    virtual void __fastcall Notification( TComponent* p, TOperation op );
    virtual void __fastcall Paint(void);
    DYNAMIC void __fastcall MouseMove(Classes::TShiftState Shift, int X, int Y);
    DYNAMIC void __fastcall MouseDown(TMouseButton Button, Classes::TShiftState Shift, int X, int Y);

  public:
    __fastcall HCustomCharImage( TComponent *Owner );

    void __fastcall Change( void );
    int  __fastcall X2N( int x );
    int  __fastcall Y2N( int y );

    bool __fastcall GetDot( int x, int y );
    void __fastcall SetDot( int x, int y, bool dot );
    void __fastcall InvDot( int x, int y );

  //RO props
         DEF_PROP_P( int,                ImageWidth,  ; )

  //Props
    DEF_PROP_VP_DEF( int,                ItemIndex,   ;, -1 )
    DEF_PROP_VP_DEF( int,                DotSize,     ;, 10 )
        DEF_PROP_VP( PHCustomFontBitmap, FontBitmap,  ; )
  //Events
        DEF_PROP_VV( TMouseMoveEvent,    OnDotMove )
        DEF_PROP_VV( TMouseEvent,        OnDotPress )
};

CLASSBASE( HCharImage, public HCustomCharImage )
  public:
    __fastcall HCharImage( TComponent *Owner );
  __published:
  //Base
    __property Color, ParentColor, ShowHint, ParentShowHint, PopupMenu,
               Enabled, Align, Anchors, Constraints, Visible,
               OnContextPopup;
  //Self
    __property ItemIndex, FontBitmap;
};

//---------------------------------------------------------------------------
class TFontEditForm : public TForm {
  __published:
        TPanel *Panel1;
        TLabel *Label1;
        TEdit *edtFile;
        TLabel *Label2;
        TSpeedButton *btnOpen;
        TSpeedButton *btnImport;
        TSpeedButton *btnExport;
        TStatusBar *status;
        TComboBox *edtOff;
        TLabel *Label3;
        TOpenDialog *open;
        TEdit *edtNLen;
        TUpDown *btnNLen;
        TLabel *Label4;
        TEdit *edtNPair;
        TUpDown *btnNPair;
        TLabel *Label5;
        TEdit *edtNChars;
        TUpDown *btnNChars;
        TPanel *panList;
        TPanel *Panel3;
        TLabel *Label6;
        TEdit *Edit1;
        TUpDown *btnZoom;
        TPanel *panDraw;
        TPanel *Panel4;
        TSplitter *Splitter1;
        TPanel *Panel2;
        TLabel *Label7;
        TEdit *Edit2;
        TUpDown *btnDotSize;
        TEdit *edtChars;
        TMemo *txtChars;
        TSplitter *Splitter2;
        void __fastcall btnImportClick(TObject *Sender);
        void __fastcall btnOpenClick(TObject *Sender);
        void __fastcall edtOffClick(TObject *Sender);
        void __fastcall FormCreate(TObject *Sender);
        void __fastcall btnZoomClick(TObject *Sender, TUDBtnType Button);
        void __fastcall btnExportClick(TObject *Sender);
        void __fastcall btnDotSizeClick(TObject *Sender,
          TUDBtnType Button);
        void __fastcall edtCharsChange(TObject *Sender);
  private:
    PHFontBitmap   font;
    PHCharFontList list;
    PHCharImage    image;

  private:
    void __fastcall idDump2Char( TObject *Sender, LPBYTE dst, LPCBYTE srs );
    void __fastcall idChar2Dump( TObject *Sender, LPBYTE dst, LPCBYTE srs );
    void __fastcall idChange( TObject *Sender );
    bool __fastcall idDrawItem(TObject *Sender, TCanvas *dc, int Index, const MyRect &Rect, const TOwnerDrawState &State);
    void __fastcall idListClick( TObject *Sender );
    void __fastcall idMouseMove( TObject *Sender, Classes::TShiftState Shift, int x, int y);
    void __fastcall idMouseDown( TObject *Sender, TMouseButton Button, Classes::TShiftState Shift, int x, int y);

  public:
    __fastcall TFontEditForm(TComponent* Owner);

};

#endif
